package com.boesch.electroOffice.presentation.customer;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.event.ActionEvent;
import javax.faces.event.ComponentSystemEvent;
import javax.portlet.PortletSession;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.boesch.electroOffice.business.businessObject.CustomerBusinessObject;
import com.boesch.electroOffice.business.common.BusinessException;
import com.boesch.electroOffice.dataaccess.common.IllegalDataAccessException;
import com.boesch.electroOffice.model.enums.UIMessages;
import com.boesch.electroOffice.model.messerli.Customer;
import com.boesch.electroOffice.utils.UserUtils;
import com.boesch.electroOffice.utils.MessageUtils;
import com.boesch.electroOffice.utils.PortletUtils;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.model.User;

/**
 * This class controls all the data is needed for the customer form.
 * 
 * For more details about the package layering have a look at the documentation
 * of the project or the project object model pom.xml
 * 
 * @author Ralph Koster
 * 
 */
@Controller
@ManagedBean
@SessionScoped
public class CustomerFormController implements Serializable {

	private static final long serialVersionUID = 776185031405995898L;
	private static final Log log = LogFactoryUtil.getLog(CustomerFormController.class);

	@Autowired
	private CustomerNavigationController customerNavigationController;
	@Autowired
	private CustomerBusinessObject customerBusinessObject;

	private Customer customer = new Customer();

	private List<String> salutations = new ArrayList<String>();
	
	public void loadData(ComponentSystemEvent event) {
		customer = new Customer();
		PortletSession session = PortletUtils.getPortletSession();
		String customerId = String.valueOf(session.getAttribute("customerId"));
		try {
			if(StringUtils.isNumeric(customerId)) {
				customer = customerBusinessObject.readCustomerByCustomerId(Integer.parseInt(customerId));
			}
		} catch (IllegalDataAccessException e) {
			log.error("Failed to load customer from database: " + customerId, e);
		} catch (BusinessException e) {
			log.error("Failed to load appointment from database: " + customerId, e);
		}		
	}
	
	public void saveCustomer(ActionEvent event) {
		User currentUser = UserUtils.getCurrentUser();
		try {
			checkNullFields();
			if(customer.getCustomerId() < 1) {
				customerBusinessObject.saveCustomer(customer, currentUser);
				MessageUtils.addInfoMessage(UIMessages.SAVE_CUSTOMER_OK.getMessage());
			} else {
				customerBusinessObject.updateCustomer(customer, currentUser);
				MessageUtils.addInfoMessage(UIMessages.UPDATE_CUSTOMER_OK.getMessage());
			}
			customerNavigationController.setNavigationPath("/jsf/customer/customer_list.xhtml");
		} catch (IllegalDataAccessException e) {
			MessageUtils.addErrorMessage(UIMessages.SAVE_CUSTOMER_ERROR.getMessage());
			log.error("Failed to save customer on database", e);
		} catch (BusinessException e) {
			MessageUtils.addErrorMessage(UIMessages.SAVE_CUSTOMER_ERROR.getMessage());
			log.error("Failed to save customer on database", e);
		}
	}

	public Customer getCustomer() {
		return customer;
	}

	public List<String> getSalutationList() {
		return salutations;
	}
	
	private void checkNullFields() {
		if(null == customer.getSalutation()) {
			customer.setSalutation("");
		}
		if(null == customer.getPoBox()) {
			customer.setPoBox("");
		}
		if(null == customer.getStreet()) {
			customer.setStreet("");
		}
		if(null == customer.getPlz()) {
			customer.setPlz("");
		}
		if(null == customer.getCity()) {
			customer.setCity("");
		}
		if(null == customer.getCountry()) {
			customer.setCountry("");
		}
		if(null == customer.getTelPrivate()) {
			customer.setTelPrivate("");
		}
		if(null == customer.getTelBusiness()) {
			customer.setTelBusiness("");
		}
		if(null == customer.getFax()) {
			customer.setFax("");
		}
		if(null == customer.getMobile()) {
			customer.setMobile("");
		}
		if(null == customer.getHomepage()) {
			customer.setHomepage("");
		}
	}
}
